package com.zhika.zishi.fragments.login;

import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableRow;

import com.loopj.android.http.AsyncHttpClient;
import com.zhika.zishi.MyApplication;
import com.zhika.zishi.R;
import com.zhika.zishi.internet.InternetTools;
import com.zhika.zishi.internet.PostInternet;

/**
 * 资势进入页
 */
public class EntryFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "EntryFragment";

    private Button loginButton;

    private TableRow browseTableRow;

    //该fragment的界面view
    private View view;

    private Context context;

    //进来时候的图片，会慢慢消失
    private ImageView entryImageView;

    //网络请求对象
    private AsyncHttpClient asyncHttpClient;

    /**
     * 在activity中实现此接口的方法，控制该fragment的事件
     */
    public interface OnEntryListener {

        //登录按钮的事件
        public void entryLoginListener();

        //浏览按钮的事件
        public void entryBrowseListener();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.entry_layout, container, false);

        //初始化界面控件
        initView();

        return view;
    }

    /**
     * 初始化界面控件
     */
    private void initView() {

        context = getActivity();

        if (!InternetTools.isNetConnected(context)){
           MyApplication.showToast("请连接网络");
        }

        //实例化网络请求对象
        asyncHttpClient = new AsyncHttpClient();

        loginButton = (Button) view.findViewById(R.id.entry_login_button);
        loginButton.setOnClickListener(this);

        browseTableRow = (TableRow) view.findViewById(R.id.entry_browse_table_row);
        browseTableRow.setOnClickListener(this);

        entryImageView = (ImageView) view.findViewById(R.id.entry_image_view);

        //进入页的动画
        ObjectAnimator.ofFloat(entryImageView, "alpha", 1, 0).setDuration(3 * 1000).start();
//        entryImageView.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
        if (context instanceof OnEntryListener) {
            switch (v.getId()) {
                case R.id.entry_login_button:
                    ((OnEntryListener) context).entryLoginListener();
                    break;
                case R.id.entry_browse_table_row:
                    ((OnEntryListener) context).entryBrowseListener();
                    break;
            }
        }

    }
}
